var prizeNum = 0; //中奖奖品下标
var prizeVal = []
var time = ''
var rotateType = true
var music = 0
window.onload = function() {
	var audio = document.getElementById("music1")
	$("#start").click(function() {
		$(this).css("background","#ccc")
		if(music == 1) {
			audio.play();
			start()
			music = 0
		} else {
			alert("暂无抽奖权限")
			music = 0
		}
	})
	//设置外框大小
	var height = $(window).height()
	var wid = parseInt(height * 0.61);
	$('.canvas_box').width(wid).height(wid)
	wid = wid * 4
	$('.can').width(wid).height(wid)
	var angleVal = 0;
	var cavvasAni = setInterval(function() {
		angleVal++
		if(angleVal == 360) {
			angleVal = 0
		}
	}, 3000)
	//开始抽奖
	function start() {
		if(rotateType == false) {
			return false
		}
		$('#canvas_main').removeClass('canvas_ani')
		rotateType = false
		$('#canvas_main').rotate({
			duration: 12000, //转动时间
			angle: angleVal, //起始角度
			animateTo: 7200 - prizeNum * 360 / prizeVal.length, //动画效果，需加载jquery.easing.min.js
			callback: function() {
				var name = prizeVal[prizeNum].name.split("元")
				name = name[0]
				$(".content h3 b").text(name)
				$(".tcc").css("display", "block")
				var dept = localStorage.getItem("hb_dept")
				setTimeout(function() {
					$.post("updateWdStatus", {
						ddDept: dept,
						status: '0'
					}, function(data) {
						if(data.code == 0) {
							$(".tcc").css("display", "none")
							$('#canvas_main').addClass('canvas_ani')
							time = setInterval(hb_message, "2000");
							rotateType = true
						}
					})
				}, 5000)
			}
		});
	}

	//转盘参数
	var canvasArr = {
		'width': wid, //转盘宽度
		'height': wid, //转盘高度
		'bgcolor': '#5a9cff', //背景颜色
		'border': wid * 0.06, //转盘边框
		'lightnum': 24, //背景灯个数
		'lightwid': wid * 0.036 //背景灯大小
	}
	//画转盘
	class htTurntable {
		constructor() {
			this.canvasbg = document.getElementsByTagName('canvas')[0];
			this.contextbg = this.canvasbg.getContext('2d');
			this.canvas = document.getElementsByTagName('canvas')[1];
			this.context = this.canvas.getContext('2d');
		}
		init() {
			this.canvasbg.width = canvasArr.width
			this.canvasbg.height = canvasArr.height
			this.canvas.width = canvasArr.width
			this.canvas.height = canvasArr.height
			this.bgfun()
			this.bglight()
			this.prize()
			this.iconfun()
			this.bglight('#FFFFFF', '#ffcf4d')
			this.bglightani() //背景灯动态
			$('.can').css('transform', 'scale(0.25)');
			$('.can').css('margin-left', -wid / 2)
			$('.can').css('margin-top', -wid / 2)
		}
		//背景
		bgfun() {
			this.contextbg.beginPath()
			let clg = this.contextbg.createLinearGradient(0, 0, 0, canvasArr.height);
			clg.addColorStop(0, '#FFBE04')
			clg.addColorStop(1, '#FFBE04')
			this.contextbg.fillStyle = clg;
			//this.contextbg.fillStyle = canvasArr.bgcolor
			this.contextbg.arc(canvasArr.width / 2, canvasArr.height / 2, canvasArr.width / 2, 0, Math.PI * 2);
			this.contextbg.closePath();
			this.contextbg.fill();

			this.contextbg.beginPath()
			this.contextbg.fillStyle = '#FFFFFF'
			this.contextbg.arc(canvasArr.width / 2, canvasArr.height / 2, canvasArr.width / 2 - canvasArr.border, 0, Math.PI * 2);
			this.contextbg.closePath();
			this.contextbg.fill();
		}
		//周边背景灯
		bglight(cor1, cor2) {
			for(let i = 0; i < canvasArr.lightnum; i++) {
				if(i % 2 == 0) {
					this.contextbg.fillStyle = cor1;
				} else {
					this.contextbg.fillStyle = cor2;
				}
				this.contextbg.beginPath()
				this.contextbg.arc(canvasArr.width / 2 + (canvasArr.width / 2 - canvasArr.border / 2) * Math.cos(360 / canvasArr.lightnum * i * Math.PI / 180), canvasArr.width / 2 + (canvasArr.width / 2 - canvasArr.border / 2) * Math.sin(360 / canvasArr.lightnum * i * Math.PI / 180), canvasArr.lightwid / 2, 0, Math.PI * 2);
				this.contextbg.closePath();
				this.contextbg.fill();
			}
		}
		//周边背景灯动态
		bglightani() {
			let that = this;
			let i = 0;
			setInterval(function() {
				i++;
				if(i % 2 == 0) {
					that.bglight('#FFFFFF', '#ffcf4d')
				} else {
					that.bglight('#ffcf4d', '#FFFFFF')
				}
			}, 600)
		}
		//扇形
		prize() {
			for(let i = 0; i < prizeVal.length; i++) {
				this.context.beginPath()
				if(i % 2 == 0) {
					this.context.fillStyle = "#FEDC74";
				} else {
					this.context.fillStyle = "#FFFFFF";
				}
				this.context.moveTo(canvasArr.width / 2, canvasArr.height / 2);
				this.context.arc(canvasArr.width / 2, canvasArr.height / 2, canvasArr.width / 2 - canvasArr.border, 2 * Math.PI / prizeVal.length * i - 0.5 * Math.PI - Math.PI / prizeVal.length, 2 * Math.PI / prizeVal.length * (i + 1) - 0.5 * Math.PI - Math.PI / prizeVal.length);
				this.context.closePath();
				this.context.fill();
			}
		}
		//画图icon文字
		iconfun() {
			let step = 2 * Math.PI / prizeVal.length
			for(let i = 0; i < prizeVal.length; i++) {
				//icon
				this.context.save()
				this.context.translate(canvasArr.width / 2, canvasArr.height / 2);
				this.context.rotate(step * (i))
				let img = new Image;
				img.src = $('#' + prizeVal[i].img).attr('src');
				this.context.drawImage(img, -prizeVal[i].wid / 2.5, -canvasArr.height / 2.4, prizeVal[i].wid / 1.3, prizeVal[i].hei);
				//文字
				let textval = prizeVal[i].text.split(',')
				for(let j = 0; j < textval.length; j++) {
					this.context.beginPath()
					this.context.font = "bold 64px Microsoft YaHei";
					this.context.textAlign = "center";
					this.context.fillStyle = "#E60413";
					this.context.fillText(textval[j], 0, -canvasArr.height / 4.3 + 80 * j);
				}
				this.context.restore()
			}
		}
	}
	//获取所有网点
	/*$.post("selectAllWdzt", function(data) {
		if(data.code == 0) {
			var rel = data.data;
			var html = '<option value="">请选择您抽奖所在的网点</option>'
			for(var i in rel) {
				html += '<option value="' + rel[i].网点ID + '">' + rel[i].网点名称 + '</option>'
			}
			$(".select").html(html)
		}
	})*/
	var hb_dept = localStorage.getItem("hb_dept")
	var text = localStorage.getItem("hb_text")
	if(hb_dept == null || hb_dept == "") {

	} else {
		$("#xzwd").css("display", "none")
		$(".dept").text("抽奖网点：" + text)
		abb()
	}
	//点击保存信息
	$(".btn").click(function() {
		var value = $(".select").val()
		var text = $(".select option:selected").text()
		if(value == "") {
			alert("请选择抽奖网点")
		} else {
			localStorage.setItem("hb_dept", value)
			localStorage.setItem("hb_text", text)
			window.location.reload()

		}
	})
	$(".dept").click(function() {
		$("#xzwd").css("display", "block")
	})
	//获取奖品
	$(document).keyup(function(event){
  		if(event.keyCode ==13){
  			if(music==1){
  				$("#start").trigger("click");
  			}else{
  				alert('还未开始抽奖')
  			}
   		 	
  		}else{
  			
  		}
	});
	function abb(code) {
		$.post("selectPrizeByCode", {
			code: "15"
		}, function(data) {
			/*YDUI.dialog.alert(JSON.stringify(data))*/
			if(data.code == 0) {
				var rel = data.data;
				for(var i in rel) {
					if(rel[i].奖品名称 == '5元优惠券') {
						prizeVal.push({
							text: '5元,优惠券',
							name: rel[i].奖品名称,
							img: "img1",
							wid: wid * 0.2,
							hei: wid * 0.1,
							id: rel[i].id_prikey
						})
						prizeVal.push({
							text: '20元,优惠券',
							img: "img3",
							name: rel[i].奖品名称,
							wid: wid * 0.2,
							hei: wid * 0.1,
							id: "001"
						})
					} else if(rel[i].奖品名称 == '10元优惠券') {
						prizeVal.push({
							text: '10元,优惠券',
							name: rel[i].奖品名称,
							img: "img2",
							wid: wid * 0.2,
							hei: wid * 0.1,
							id: rel[i].id_prikey
						})
						prizeVal.push({
							text: '30元,优惠券',
							img: "img4",
							name: rel[i].奖品名称,
							wid: wid * 0.2,
							hei: wid * 0.1,
							id: "002"
						})
					} else if(rel[i].奖品名称 == '20元优惠券') {
						prizeVal.push({
							text: '20元,优惠券',
							img: "img3",
							name: rel[i].奖品名称,
							wid: wid * 0.2,
							hei: wid * 0.1,
							id: rel[i].id_prikey
						})
						prizeVal.push({
							text: '5元,优惠券',
							name: rel[i].奖品名称,
							img: "img1",
							wid: wid * 0.2,
							hei: wid * 0.1,
							id: "003"
						})
					} else if(rel[i].奖品名称 == '30元优惠券') {
						prizeVal.push({
							text: '30元,优惠券',
							img: "img4",
							name: rel[i].奖品名称,
							wid: wid * 0.2,
							hei: wid * 0.1,
							id: rel[i].id_prikey
						})
						prizeVal.push({
							text: '10元,优惠券',
							name: rel[i].奖品名称,
							img: "img2",
							wid: wid * 0.2,
							hei: wid * 0.1,
							id: "004"
						})
					} else if(rel[i].奖品名称 == '50元优惠券') {
						prizeVal.push({
							text: '50元,优惠券',
							img: "img5",
							name: rel[i].奖品名称,
							wid: wid * 0.2,
							hei: wid * 0.1,
							id: rel[i].id_prikey
						})
					} else if(rel[i].奖品名称 == '100元优惠券') {
						prizeVal.push({
							text: '100元,优惠券',
							name: rel[i].奖品名称,
							img: "img6",
							wid: wid * 0.2,
							hei: wid * 0.1,
							id: rel[i].id_prikey
						})
					}
				}
				new htTurntable().init()
				//调用中奖信息
				time = setInterval(hb_message, "2000");
			} else {
				YDUI.dialog.alert(data.message)

			}
		})
	}

	function hb_message() {
		/*let dept = localStorage.getItem("hb_dept")*/
		$.post("selectAllWdzt", {
			ddDept: hb_dept,
			activityType: '15'
		}, function(data) {
			if(data.code == 0) {
				var rel = data.data[0]
				var zt = rel.抽奖状态
				var num = rel.备注
				var arr = rel.奖品列表;
				var html = ''
				for(var i in arr) {
					html += '<li>' + arr[i].奖品名称 + ': ' + arr[i].剩余数量 + '</li>'
				}
				$(".list").html(html)
				if(zt == 1) {
					for(var i in prizeVal) {
						if(prizeVal[i].id == num) {
							prizeNum = i
						}
					}
					clearInterval(time);
					music=1
					$("#start").css("background","#169FE6")
				} else {
					music=0
					
					$("#start").css("background","#ccc")
				}
			}
		})
	}
}